package com.atguigu.tingshu.account.mapper;

import com.atguigu.tingshu.model.account.UserAccount;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Update;

import java.math.BigDecimal;

@Mapper
public interface UserAccountMapper extends BaseMapper<UserAccount> {

    /**
     * 新增账户可用余额
     * @param userId
     * @param intValue
     */
    @Update("UPDATE user_account SET available_amount = available_amount + #{intValue} WHERE user_id = #{userId} AND is_deleted = 0")
    void addAvailableAmount(@Param("userId") Long userId,
                            @Param("intValue") int intValue);

    /**
     * 余额支付扣款
     * @param userId
     * @param price
     * @return
     */
    @Update("UPDATE user_account SET available_amount = available_amount - #{price} WHERE available_amount >= #{price} AND user_id = #{userId}")
    int updateAccountAvailableAmount(@Param("userId") Long userId,
                                     @Param("price") BigDecimal price);
}
